import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/phoenix/home/bean/home_chart_bean.dart';
import 'package:dk_flutter_module_sale/phoenix/home/bean/work_home_bean.dart';
import 'package:flutter/material.dart';
import '../bean/ui_const_bean.dart';
import 'circle_progress_widget.dart';
import 'work_head_widget.dart';

class WorkMutiChartWidget extends StatelessWidget {

  final Function(HomeChartBean)   chartTap;
  final UpdateModuleListBean      updateModuleBean;
  final HomeChartBean             homeChartBean;

  WorkMutiChartWidget(this.updateModuleBean,this.homeChartBean,this.chartTap);

  @override

  Widget build(BuildContext context) {
    return buildedNewWidget(context);
  }

  Widget buildedNewWidget(BuildContext context) {
    return GestureDetector(
      child: Container(
        height: UIConstBean.HomeChartH,
        color: Colors.transparent,
        padding: const EdgeInsets.only(left: UIConstBean.LRSpace, right: UIConstBean.LRSpace, top: 10),
        child: Card(
          color: Colors.white,
          elevation: 0.0,
          clipBehavior: Clip.antiAliasWithSaveLayer,
          margin: EdgeInsets.only(bottom: 0),
          shape: const RoundedRectangleBorder(
            side: BorderSide.none,
            borderRadius: BorderRadius.all(Radius.circular(UIConstBean.CircularRadius),
            ),
          ),
          child: Container(
              padding:const EdgeInsets.only(left: UIConstBean.InnerLRSpace,right: UIConstBean.InnerLRSpace,top: UIConstBean.InnerTopSpace),
              color: Colors.white,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  WorkHeadWidget(itemHeadTap: null, title: updateModuleBean.title??'',
                    indicateType: homeChartBean!=null ? HomeHeadTitleIndicateType.HomeHeadTitleIndicateLookAtType : HomeHeadTitleIndicateType.HomeHeadTitleIndicateDefaultType,),
                  SizedBox(height: 14,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: _buildChartWidget(),
                  )
                ],
              )
          ),
        ),
      ),
      onTap: () {
        print('----点击图表----');
        if(chartTap!= null) {
          chartTap(homeChartBean);
        }
      },
    );
  }

  List<Widget> _buildChartWidget(){

    List<Widget> widgets = List<Widget>();
    if(homeChartBean == null) {
      widgets.add(_buildOneChartWidget(null,Color(0xff749DFD), Color(0xffA951D4)));
      widgets.add(SizedBox(width: 80,));
      widgets.add(_buildOneChartWidget(null,Color(0xffFF9E53), Color(0xffFFCE45)));
      return widgets;
    }
    for(int i = 0; i < 2; i++) {
      ChartBean chartBean =  homeChartBean.list[i];
      if(i == 0) {
        widgets.add(_buildOneChartWidget(chartBean,Color(0xff749DFD), Color(0xffA951D4)));
        widgets.add(SizedBox(width: 80,));
      } else {
        widgets.add(_buildOneChartWidget(chartBean,Color(0xffFF9E53), Color(0xffFFCE45)));
      }
    }
    return widgets;
  }

  Widget _buildOneChartWidget(ChartBean chartBean,Color startColor,Color endColor) {
    if(chartBean == null) {
      return _buildRowChartWidget(0, startColor, endColor, '收房目标', '', '成交数', '');
    }
    ChartItemBean oneBean = chartBean.items[0];
    ChartItemBean twoBean = chartBean.items[1];
    return _buildRowChartWidget(chartBean.precentage, startColor, endColor, oneBean.title, oneBean.amount.toString(), twoBean.title, twoBean.amount.toString());
  }

  Widget _buildRowChartWidget(double progress,
      Color startColor,Color endColor,
      String oneTitle, String oneAmount,
      String twoTitle, String twoAmount) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          width: 100,
          height: 100,
          color: Colors.white,
          padding: EdgeInsets.only(left: 0,top: 0),
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              Offstage(
                child:  RichText(text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(text:'${(progress * 100).round()}',
                        style: TextStyle(fontSize: 28.0,
                          color: Color(0xff4a5261),fontWeight: FontWeight.w600,
                        ),
                      ),
                      TextSpan(text:'%',
                        style: TextStyle(fontSize: 12.0,
                          color: Color(0xff4a5261),fontWeight: FontWeight.w600,
                        ),
                      ),
                    ]
                )
                ),
                offstage: progress < 0.0001,
              ),

              CircleProgressWidget(
                radius: 45.0,
                ringColor: Color(0xffF1F1F1),
                dotRadius: 4.0,
                shadowWidth: 0.0,
                progress: progress,
                startColor: startColor,
                endColor: endColor,
              ),
            ],
          ),
        ),
        SizedBox(height:12),
        _buildRichText(oneTitle, oneAmount),
        SizedBox(height:4),
        _buildRichText(twoTitle, twoAmount),
      ],
    );

  }

  RichText _buildRichText( String title, String amount) {
    return  RichText(text: TextSpan(
        children: <TextSpan>[
          TextSpan(
            text: title+' ',
            style: TextStyle(fontSize: 13.0,
              color: Color(0xff666666),fontWeight: FontWeight.normal,
            ),
          ),
          TextSpan(
            text:amount,
            style: TextStyle(fontSize: 14.0,
              color: Color(0xff333333),fontWeight: FontWeight.bold,
            ),
          ),
        ]
    ),
    );
  }
}

